/*
 * @Author: jiangxin
 * @Date: 2022-11-25 14:32:11
 * @Company: orientsec.com.cn
 * @Description:
 */
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");

const currentScript = process.env.npm_lifecycle_event;

// filename:业务名称
// env:开发[dev]/生产[prod]
// action:动作 serve/build
const {
  groups: { filename, env, action = "serve" }
} = currentScript?.match?.(
  /(?<filename>\w+-{1}\w+)-{1}(?<env>\w+)(:{1}(?<action>\w+))?/i
);

module.exports = () => {
  return {
    entry: {
      [filename]: { import: `./src/${filename}/index.jsx` }
    },
    output: {
      clean: true,
      path: path.resolve(__dirname, "dist/", filename),
      filename: "app-[contenthash:16].js"
    },
    module: {
      rules: [
        {
          test: /\.(js|jsx)$/i,
          loader: "babel-loader"
        },
        {
          test: /\.s[ac]ss$/i,
          use: ["style-loader", "css-loader", "postcss-loader", "sass-loader"]
        },
        {
          test: /\.css$/i,
          use: ["style-loader", "css-loader", "postcss-loader"]
        },
        {
          test: /\.(eot|svg|ttf|woff|woff2|png|jpg|gif)$/i,
          type: "asset"
        }
      ]
    },
    plugins: [
      new HtmlWebpackPlugin({
        template: "index.html",
      })
    ],
    resolve: {
      extensions: [".jsx", "..."]
    }
  };
};

